package com.yuri.news.controller;


import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuri.news.domain.Pages;
import com.yuri.news.domain.Result;
import com.yuri.news.entity.Classify;
import com.yuri.news.service.ClassifyService;
/**
 * 分类控制器
 * @author Yuri
 *
 */
//允许跨域请求
@CrossOrigin
// 以json形式返回
@ResponseBody
// 控制器
@Controller
@SuppressWarnings("deprecation")
public class ClassifyController {
	// 日志打印
	private final Logger logger = LoggerFactory.getLogger(ClassifyController.class);
	@Autowired
	ClassifyService classifyService;
	/**
	 * 添加分类
	 * 
	 * @param classify
	 */
	@RequestMapping(value = "addClassify", method = RequestMethod.POST)
	public Result<Classify> addClassify(Classify classify) {
		logger.info("\n 添加分类（addClassify）开始: Classify = " + classify.toString());
		// 如果 此分类的上级分类不为空  并且 上级分类的id为-1 就设置为空
		Result<Classify> result = new Result<>();
		try {
			Classify cfy = classifyService.saveClassify(classify);
			if(cfy != null) {
				result.setSuccess("保存成功！");
			}else {
				result.setError("保存失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 添加分类（addClassify）结束: result" + result);
		return result;
	}
	/**
	 * 更新分类信息
	 * @param classify
	 * @return
	 */
	@RequestMapping(value = "updateClassify", method = RequestMethod.PUT)
	public Result<Classify> updateClassify(Classify classify){
		logger.info("\n 更新分类信息(updateClassify)开始 classify =" + classify);
		Result<Classify> result = new Result<>();
		
		try {
			if(classifyService.updateClassify(classify)) {
				result.setSuccess("更新成功！");
			}else {
				result.setError("无此分类!");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误!");
		}
		logger.info("\n 更新分类信息(updateClassify)结束 result = " + result);
		return result;
	}

	/**
	 * 根据id删除分类
	 * 
	 * @param classify
	 */
	@RequestMapping(value = "delClassify", method = RequestMethod.DELETE)
	public Result<Classify> delClassify(Long id, Boolean flag) {
		// 日志打印
		logger.info("\n 根据id删除分类(delClassify)开始 Classify.id = " + id);
		Result<Classify> result = new Result<>();
		try {
			if(classifyService.deleteClassify(id, flag)) {
				result.setSuccess("删除成功！");
			}else {
				result.setError("无此分类信息！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据id删除分类(delClassify)结束  Classify.id = " + id);
		return result;
	}

	/**
	 * 根据id查询该分类下的分类
	 * 
	 * @param id
	 */
	@RequestMapping(value = "findDowClass", method = RequestMethod.GET)
	public Result<Classify> findDowClass(Long id) {
		logger.info("\n 根据id查询该分类下的所有分类(findDowClass:)开始 Classify.id = " + id);
		Result<Classify> result = new Result<>();
		
		try {
			List<Classify> classifies = null;
			classifies = classifyService.listDowClassify(id);
			if(classifies != null) {
				result.setData(classifies);
				result.setSuccess("查询成功！");
			}else {
				result.setError("暂时无数据，请稍后再试");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据id查询该分类下的所有分类(findDowClass:)结束 result = " + result);
		return result;
	}

	/**
	 * 根据id查询分类信息
	 * 
	 * @param id
	 */
	@RequestMapping(value = "findClassById", method = RequestMethod.GET)
	public Result<Classify> findClassById(Long id) {
		logger.info("\n 根据id查询分类信息(findClassById:)开始 Classify.id = " + id);
		Result<Classify> result = new Result<>();
		
		try {
			List<Classify> classifies = new ArrayList<>();
			Classify classify = classifyService.getClassifyById(id);
			
			if(classify != null) {
				classifies.add(classify);
				result.setData(classifies);
				
				result.setSuccess("查询成功！");
			}else {
				result.setError("无此分类信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据id查询分类信息(findClassById:)结束 result = " + result);
		return result;
	}

	/**
	 * 查询所有分类信息
	 * @param page		分页信息
	 * @param size		每页长度
	 * @return
	 */
	@RequestMapping(value = "findClassByAll", method = RequestMethod.GET)
	public Result<Classify> findClassByAll(
			@RequestParam(name = "page",defaultValue = "0") Integer page,
			@RequestParam(name = "size", defaultValue= "100") Integer size) {
		Order order 	= new Order(Direction.ASC, "id"); // 根据字段id进行升序排序
		@SuppressWarnings("unused")
		PageRequest pageRequest = PageRequest.of(page, size, new Sort(order));// 分页排序查询
		
		logger.info("\n 查询所有分类信息(findClassByAll:)开始  参数page = " + page + "\t size = " + size);
		Result<Classify> result = new Result<>();
		// 分页信息
		try {
			
			List<Classify> classifies = classifyService.listClassify();
			
			if(classifies != null && classifies.size() > 0) {
				result.setData(classifies);
				result.setSuccess("查询成功！");
			}else {
				result.setError("暂时无分类信息，请稍后再试");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		// 日志打印
		logger.info("\n 查询所有分类信息(findClassByAll:)结束 result = " + result);
		return result;
	}
	
	/**
	 * 查询一级分类分类信息
	 * @param page		当前页
	 * @param size		每页大小
	 * @return
	 */
	@RequestMapping(value = "findClassByAllOne", method = RequestMethod.GET)
	public Result<Classify> findClassByAllOne(
			@RequestParam(name = "page",defaultValue = "0") Integer page,
			@RequestParam(name = "size", defaultValue= "10") Integer size) {
		logger.info("\n 查询一级分类分类信息(findClassByAllOne)开始 参数 page = " + page + "\t size = " +size);
		// 返回数据
		Result<Classify> result = new Result<>();
		
		try {
			// 分页信息
			Order order 	= new Order(Direction.ASC, "id"); // 根据字段id进行升序排序
			
			PageRequest pageRequest = PageRequest.of(page, size, new Sort(order));// 分页排序查询
			Page<Classify> classPage = classifyService.listClassByAllOne(pageRequest);
			
			if(classPage != null && classPage.getContent() != null && classPage.getContent().size() > 0) {
				Pages pages = new Pages(page, classPage.getTotalElements(), size);
				
				result.setPages(pages);
				result.setData(classPage.getContent());
				result.setSuccess("查询成功!");
			}else {
				result.setError("暂无分类信息！请稍后再试");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 查询所有一级分类分类信息(findClassByAllOne:)结束 result = " + result);
		return result;
	}
}
